﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TJDevHouse.Starcraft.Game
{
	public class Unit
	{
		public int ID { get; private set; }

		public Game Game { get; set; }

		public Player Player { get; set; }

		public int LastSeenFrameNumber { get; set; }

		public UnitType UnitType { get; set; }
		public UnitTypeSpec UnitTypeSpec
		{
			get
			{
				return Game.UnitTypeSpecs[UnitType];
			}
		}

		/// <summary>
		/// Whether this unit is owned by an enemy of the client player.
		/// </summary>
		public bool IsEnemyUnit
		{
			get
			{
				return Player.IsEnemy;
			}
		}

		/// <summary>
		/// Whether this unit is owned by the client player.
		/// </summary>
		public bool IsMyUnit
		{
			get
			{
				return Player == Game.ClientPlayer;
			}
		}

		/// <summary>
		/// Whether this unit is owned by an ally of the client player.
		/// 
		/// Will return true if owned by the client player
		/// </summary>
		public bool IsAllyUnit
		{
			get
			{
				return Player.IsAlly;
			}
		}

		/// <summary>
		/// Is this unit owned by the neutral player
		/// </summary>
		public bool isNeutralUnit
		{
			get
			{
				return Player.IsNeutral;
			}
		}

		/// <summary>
		/// The number of unit hit points
		/// </summary>
		public int HitPoints { get; set; }

		/// <summary>
		/// The amount of shields remaining on unit (protoss only?)
		/// </summary>
		public int Shields { get; set; }

		/// <summary>
		/// The unit's energy
		/// </summary>
		public int Energy { get; set; }

		/// <summary>
		/// The amount of resources held by this unit, e.g. the amount
		/// of gas remaining in a geyser.
		/// </summary>
		public int Resources { get; set; }

		public PixelMapCoordinate PixelCoord { get; set; }
		public WalkTileMapCoordinate WalkTileCoord
		{
			get
			{
				return PixelCoord.GetWalkTileCoordinate();
			}
		}
		public BuildTileMapCoordinate BuildTileCoord
		{
			get
			{
				return PixelCoord.GetBuildTileCoordinate();
			}
		}

		public Order Order { get; set; }

		public bool IsBeingConstructed { get; set; }
		public bool IsBlind { get; set; }
		public bool IsBurrowed { get; set; }
		public bool IsCarryingGas { get; set; }
		public bool IsCarryingMinerals { get; set; }
		public bool IsCloaked { get; set; }
		public bool IsConstructing { get; set; }
		public bool IsGatheringGas { get; set; }
		public bool IsGatheringMinerals { get; set; }
		public bool IsRepairing { get; set; }
		public bool IsResearching { get; set; }
		public bool IsSieged { get; set; }
		public bool IsTraining { get; set; }
		public bool IsUnpowered { get; set; }
		public bool IsUpgrading { get; set; }

		public Unit(int id)
		{
			ID = id;
			LastSeenFrameNumber = 0;

			UnitType = UnitType.Type_None;
			HitPoints = 0;
			Shields = 0;
			Energy = 0;
			Resources = 0;
			PixelCoord = new PixelMapCoordinate(0, 0);
			Order = Order.None;

			ClearStatuses();
		}

		/// <summary>
		/// Sets all status flags to false.
		/// </summary>
		public void ClearStatuses()
		{
			IsBeingConstructed = false;
			IsBlind = false;
			IsBurrowed = false;
			IsCarryingGas = false;
			IsCarryingMinerals = false;
			IsCloaked = false;
			IsConstructing = false;
			IsGatheringGas = false;
			IsGatheringMinerals = false;
			IsRepairing = false;
			IsResearching = false;
			IsSieged = false;
			IsTraining = false;
			IsUnpowered = false;
			IsUpgrading = false;
		}
	}
}
